package com.intellagric.service.traceability.Impl.trace_field;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.traceability.FieldDao;
import com.intellagric.mapper.TraceFieldMapper;
import com.intellagric.module.traceability.FieldVo;
import com.intellagric.pojo.TraceField;
import com.intellagric.pojo.TraceFieldExample;
import com.intellagric.service.traceability.trace_field.FieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/11/6
 * @DESC： 场地信息服务
 */
@Service
public class FieldServiceImpl implements FieldService {

    @Autowired
    private TraceFieldMapper fieldMapper;

    @Autowired
    private FieldDao fieldDao;

    /**
     * 添加场地信息
     * @param field
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addField(TraceField field) {
        if (fieldMapper.insertSelective(field) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除场地信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteField(String id) {
        if (fieldMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改场地信息
     * @param field
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editField(TraceField field) {
        if (fieldMapper.updateByPrimaryKeySelective(field) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询场地信息
     * @param id
     * @return TraceField
     */
    @Override
    public TraceField getFieldById(String id) {
        return fieldMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询场地信息列表
     * @return TraceField
     */
    @Override
    public LayuiDataGridResult getFieldList() {
        TraceFieldExample fieldExample = new TraceFieldExample();
        fieldExample.createCriteria().andIdIsNotNull();
        List<TraceField> fieldList = fieldMapper.selectByExample(fieldExample);
        return LayuiDataGridResult.success().add(fieldList,fieldList.size());
    }

    /**
     * 分页查询场地信息列表
     * @return TraceField
     */
    @Override
    public LayuiDataGridResult getFieldPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TraceFieldExample fieldExample = new TraceFieldExample();
        fieldExample.createCriteria().andIdIsNotNull();
        List<TraceField> fieldList = fieldMapper.selectByExample(fieldExample);
        return LayuiDataGridResult.success().add(fieldList,fieldList.size());
    }
    /**
     * 分页查询场地信息列表
     * @return TraceField
     */
    @Override
    public LayuiDataGridResult getFieldPage(int page, int rows,String createBy) {
        PageHelper.startPage(page,rows);
        TraceFieldExample fieldExample = new TraceFieldExample();
        fieldExample.createCriteria().andIdIsNotNull().andCreateByEqualTo(createBy);
        List<TraceField> fieldList = fieldMapper.selectByExample(fieldExample);
        return LayuiDataGridResult.success().add(fieldList,fieldList.size());
    }

    /**
     * 通过VO类查询场地信息列表
     * @return TraceField
     */
    @Override
    public LayuiDataGridResult getFieldByVo(FieldVo fieldVo) {
        List<TraceField> fieldList = fieldDao.selectFieldByVo(fieldVo);
        return LayuiDataGridResult.success().add(fieldList,fieldList.size());
    }
    /**
     * 通过VO类查询场地信息列表
     * @return TraceField
     */
    @Override
    public LayuiDataGridResult getFieldByVo(int page,int limit,FieldVo fieldVo) {
        PageHelper.startPage(page,limit);
        List<TraceField> fieldList = fieldDao.selectFieldByVo(fieldVo);
        return LayuiDataGridResult.success().add(fieldList,fieldList.size());
    }
}
